	
*-------------------------------------------------------------------------------
* calculate counterfactuals
*-------------------------------------------------------------------------------

* traffic stop data
use "data/outputs/TSSS_beat.dta", clear
rename beatlocation beat
drop if beat == "1651" | beat == "1652" | beat == "1653" |  beat == "1655" /* O'Hare */
drop if beat == "6100" /* Non-geographic beat */
drop if beat == ""
keep if agencyname == "CHICAGO POLICE"
keep beat month count black
renvars black count, prefix(itss)

* drop covid period
drop if month >= tm(2020m3)

* percent of traffic stops in which driver is Black
gen pct_black_traffic = itssblack / itsscount

* percent of traffic stops in which driver is Black, PRE-2015 JUNE
gen pct_black_pre = pct_black_traffic if month < tm(2015m6) 
egen mean_pctblack_pre = mean(pct_black_pre), by(beat)
	/* Note: This mean excludes months with zero stops,
			 for which % black is undefined. */
drop pct_black_pre

* counterfactual: racial composition in the absence of within-beat changes
gen cntrfctl_acrossonly_itssblack = itsscount * mean_pctblack_pre
gen pctblack_ctfl_beat = cntrfctl_acrossonly_itssblack / itsscount

/* check that this is working
	local beatx = 1533
	twoway (connected pct_black_traffic month if beat == "`beatx'", mcolor(black) lcolor(black)) ///
		(connected pctblack_ctfl_beat month if beat == "`beatx'", mcolor(red) lcolor(red)) 
	*/

* collapse to get citywide counterfactual
collapse (sum) itssblack cntrfctl_acrossonly_itssblack itsscount, by(month)

* actual and counterfactual 
gen pctblack_actual = itssblack / itsscount
gen pctblack_ctfl = cntrfctl_acrossonly_itssblack / itsscount
replace pctblack_ctfl = pctblack_actual if month < tm(2015m8)


*-------------------------------------------------------------------------------
* graph
*-------------------------------------------------------------------------------

* line marking pre-treatment baseline
qui su pctblack_actual if month < tm(2015m8)
local base = `r(mean)'
gen base = `base'

* shaded region
gen treatment = 1 if (month >= tm(2015m8) & month <= tm(2016m1))

* graph
#delimit;
twoway (area treatment month if month >= tm(2012m1) & month <= tm(2020m2), 
	lcolor(gs15%100) fcolor(gs15%100))
(rarea base pctblack_actual month if month >= tm(2015m8), color(orange%50))
(rarea pctblack_ctfl pctblack_actual month, color(green%60))  
(line base month, color(orange%75)) 
(connected pctblack_actual month, mcolor(black) lcolor(black) msize(vsmall))
(line pctblack_ctfl month, lcolor(black) lpattern(dash) ), 
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Proportion of Stopped Drivers Who Are Black", size(medlarge))
yscale(lcolor(none))
ylabel(0(.2)1, labsize(medlarge) glcolor(white) angle(horizontal))
xtitle("", size(medsmall)) 
xscale(lcolor(none))
xlabel(624(12)723, labsize(medlarge) format(%tmCY)) 
legend(off);
graph export "figures/FigureS5.pdf", replace;


* End
